﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class EnterTypeController : Controller
    {
        public ActionResult EnterTypeList()
        {
            return View();
        }
        //查询
        public ActionResult List(BD_EnterType EnterType)
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            listSearch.Add(new SearchCondition() { Field = "DelState", Operator = "=", Value = "0" });
            Expression<Func<BD_EnterType, bool>> exp = SearchExpression.FindByGroup<BD_EnterType>(EnterType, listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            var list = BLLSession.Current.BD_EnterTypeBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, t => t.ID, true);
            count = BLLSession.Current.BD_EnterTypeBLL.GetCount(exp);
            return Json(new EasyuiDataPager() { rows = list, total = count });
        }
        //添加
        public ActionResult AddEnterType(BD_EnterType ET)
        {
            var list1 = DALSession.Current.BD_EnterTypeDAL.GetListBy(d => d.EnterType == ET.EnterType && d.DelState == "0");
            if (list1.Count == 0)
            {
                ET.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                ET.EditPerName = OperateContext.sessUserInfo.Name;
                ET.EditPerID = OperateContext.sessUserInfo.Id;
                ET.DelState = "0";
                BLLSession.Current.BD_EnterTypeBLL.Add(ET);
                return Json(new Message() { msg = "保存成功", state = "success" });
            }
            else
            {
                return Json(new Message() { msg = "数据已存在！", state = "warning" });
            }
        }
        //修改
        public ActionResult EditEnterType(BD_EnterType ET)
        {
            ET.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            ET.EditPerName = OperateContext.sessUserInfo.Name;
            ET.EditPerID = OperateContext.sessUserInfo.Id;
            BLLSession.Current.BD_EnterTypeBLL.ModifyBy(ET, t => t.ID == ET.ID, "EnterType", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        //删除
        public ActionResult DelEnterType(int Id)
        {
            //BLLSession.Current.BD_EnterTypeBLL.DelBy(d => d.ID == Id);//直接删除
            BD_EnterType BD_EnterType1 = DALSession.Current.BD_EnterTypeDAL.GetListBy(d => d.ID == Id).FirstOrDefault();
            BD_EnterType1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BD_EnterType1.EditPerName = OperateContext.sessUserInfo.Name;
            BD_EnterType1.EditPerID = OperateContext.sessUserInfo.Id;
            BD_EnterType1.DelState = "1";
            BLLSession.Current.BD_EnterTypeBLL.ModifyBy(BD_EnterType1, t => t.ID == BD_EnterType1.ID, "EnterType", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "删除成功", state = "success" });
        }
    }
}
